package cn.wgx.module.ent.web;

import cn.wgx.common.base.IBaseController;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.entity.sys.SysUser;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.common.util.ResponseDataUtil;
import cn.wgx.module.ent.constant.PublicEnterprise;
import cn.wgx.module.ent.service.EnterpriseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "5.0 企业管理")
@RestController
@RequestMapping("/api/enterprise")
public class EnterpriseCtrl extends IBaseController<EnterpriseService, Enterprise> {


    @ApiOperation(value = "根据实体参数查询列表,分页查询(page第几页, limit每页数量,默认20)")
    @GetMapping("list")
    public Map list(@ModelAttribute Enterprise t,
                    @RequestParam(required = false, defaultValue = "1") Integer page,
                    @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.page(page1));
    }

    @ApiOperation(value = "新增企业", notes = "必填：name")
    @PostMapping("add")
    public Map add(@RequestBody Enterprise enterprise) {
        if (null != service.getEntByName(enterprise.getName())) {
            return ResponseDataUtil.returnErrorMap("企业名称已被占用");
        }
        return ResponseDataUtil.returnData(service.save(enterprise));
    }

    @ApiOperation(value = "根据主键ID修改实体数据,可局部修改",notes = "必填：ent_id")
    @PatchMapping("update")
    public Map update(@RequestBody Enterprise enterprise) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        if (!loginAppUser.getUser_id().equals(service.getById(enterprise.getEnt_id()).getCreateUserId())) {
            return ResponseDataUtil.returnErrorMap("只有管理员可以更改");
        }
        return ResponseDataUtil.returnData(service.updateById(enterprise));
    }

    @ApiOperation(value = "查询未加入企业的用户,分页查询(page第几页, limit每页数量,默认20)",notes = "必填：ent_id。nickname支持模糊查询")
    @GetMapping("listUserNotInEnt")
    public Map listUserNotInEnt(@RequestParam Integer ent_id,
                                @RequestParam(required = false) String nickname,
                                @RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "20") Integer limit) {
        Page<SysUser> page1 = new Page(page, limit);
        return ResponseDataUtil.returnData(page1, service.getUserNotInEnt(page1, ent_id, nickname));
    }


    @ApiOperation(value = "根据主键ID删除数据",notes = "必填：ent_id")
    @DeleteMapping("del/{id}")
    public Map del(@PathVariable Integer id) {
        if (PublicEnterprise.ENT_ID.equals(id)) {
            return ResponseDataUtil.returnErrorMap("删除失败.个人空间不可以删除.");
        }
        return ResponseDataUtil.returnData(service.removeById(id));
    }

    @ApiOperation(value = "根据主键ID删除数据-批量",notes = "必填：ent_id")
    @DeleteMapping("delBatch")
    public Map delBatch(@RequestBody List<Integer> ids) {
        if (ids.contains(PublicEnterprise.ENT_ID)) {
            return ResponseDataUtil.returnErrorMap("删除失败.个人空间不可以删除.");
        }
        return ResponseDataUtil.returnData(service.removeByIds(ids));
    }
}
